/*
	File: playcap.cpp

	Version
	0.1 Creating a window
*/

#include "playcap.h"

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hInstp, LPSTR lpCmdLine, int nCmdShow)
{
	closed = false;

	MSG msg={0};
	WNDCLASS wc;

	//Initialize COM
	if(FAILED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)))
	{
		Msg(TEXT("CoInitialize Failed!\r\n"));
	}

	//Register the window class
	ZeroMemory(&wc, sizeof wc);
	wc.lpfnWndProc = WndMainProc;
	wc.hInstance = hInstance;
	wc.lpszClassName = CLASSNAME;
	wc.lpszMenuName = NULL;
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hIcon = NULL;

	if(!RegisterClass(&wc))
	{
		Msg(TEXT("REgisterClass Failed! Error=0x%^x\r\n"), GetLastError());
		CoUninitialize();
		exit(1);
	}

	//Create the main window. The WS_CLIPCHILDREN style is required
	ghApp = CreateWindow(CLASSNAME, APPLICATIONNAME, WS_OVERLAPPEDWINDOW | WS_CAPTION | WS_CLIPCHILDREN, 
						 CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0, 0, hInstance, 0);

	//myTextBox = CreateWindow(TEXT("STATIC"), TEXT("Label"), WS_CHILD|WS_VISIBLE|SS_LEFT, 10, 10, 25, 25, ghApp, textBoxHandle, hInstance, NULL);
	myStartButton = CreateWindow(TEXT("myStartButton"),TEXT("myStartButton"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 50, 50, 35, 35, ghApp, myStartButtonHandle, hInstance, NULL);

	if(ghApp)
	{
		ShowWindow(ghApp, nCmdShow);

		while(GetMessage(&msg, NULL, 0, 0))
		{
			if(!TranslateAccelerator(ghApp,NULL,&msg))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}		
	}

	//release COM
	CoUninitialize();

	return (int) msg.wParam;

}

void Msg(TCHAR *szFormat, ...)
{
	//This function will  create a message box to create a message


	TCHAR szBuffer[1024];
	const size_t NUMCHARS = sizeof(szBuffer)/sizeof(szBuffer[0]);
	const int LASTCHAR = NUMCHARS - 1;

	//va_list: a variable argument list, holds information about variable arguments
	//va_start: initialize va_list with arguments after the second parameter in this case szFormat
	va_list pArgs;
	va_start(pArgs, szFormat);

	//StringCchVPrint: writes data to the specified string using a pointer to a list of arguments
	//Takes the char destination buffer, the size of the buffer, the null-terminated input string,
	//and the list of arguments.
	(void)StringCchVPrintf(szBuffer, NUMCHARS - 1, szFormat, pArgs);

	//Stop using variable list
	va_end(pArgs);

	//add the null character to the buffer
	szBuffer[LASTCHAR] = TEXT('\0');

	MessageBox(NULL, szBuffer, TEXT("PlayCap Message"), MB_OK | MB_ICONERROR);
}

LRESULT CALLBACK WndMainProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }

    return DefWindowProc (hwnd , message, wParam, lParam);
}